{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "from datasets import load_dataset\n",
    "\n",
    "ds = load_dataset(\"PrimeIntellect/verifiable-coding-problems\", split=\"train\", trust_remote_code=True)\n",
    "print(ds[0].keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ast\n",
    "from pprint import pprint\n",
    "\n",
    "dataset = []\n",
    "for entry in ds:\n",
    "    gold_standard_solution = entry[\"gold_standard_solution\"]\n",
    "    if gold_standard_solution is None:\n",
    "        continue\n",
    "    if gold_standard_solution.startswith(\"```python\") and gold_standard_solution.endswith(\"```\"):\n",
    "        tests = entry[\"verification_info\"]\n",
    "        if isinstance(tests, str):\n",
    "            try:\n",
    "                tests = ast.literal_eval(tests)\n",
    "            except (ValueError, SyntaxError):\n",
    "                # Try Json loads instread\n",
    "                try:\n",
    "                    tests = json.loads(entry[\"verification_info\"])\n",
    "                except (json.JSONDecodeError, SyntaxError, ValueError) as e:\n",
    "                    print(repr(entry[\"verification_info\"]))\n",
    "                    print(f\"Error in json.loads: {e}\")\n",
    "                    continue\n",
    "        assert isinstance(tests, dict), \"Tests should be a dictionary\"\n",
    "        assert tests[\"language\"] == \"python\"\n",
    "        tests = tests[\"test_cases\"]\n",
    "        if len(tests) <= 4:\n",
    "            continue\n",
    "        new_entry = {\n",
    "            \"problem\": entry[\"prompt\"],\n",
    "            \"solutions\": [gold_standard_solution],\n",
    "            \"tests\": tests,\n",
    "        }\n",
    "\n",
    "        # Assert tests is a dictionary\n",
    "        assert isinstance(tests, list), \"Tests should be a dictionary\"\n",
    "        assert \"input\" in tests[0], \"Tests should have an input key\"\n",
    "        assert \"output\" in tests[0], \"Tests should have an output key\"\n",
    "\n",
    "        dataset.append(new_entry)\n",
    "\n",
    "print(len(dataset))\n",
    "print(dataset[0])\n",
    "dataset = dataset\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"primeintellect.json\")\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"../../../../tests/rllm/rewards/primeintellect_test_err.json\") as f:\n",
    "    bad_problems = json.load(f)\n",
    "len(bad_problems)\n",
    "\n",
    "pprint(bad_problems[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rllm.utils import RAG\n",
    "\n",
    "good_problems = [True] * len(dataset)\n",
    "rag = RAG(docs=[r[\"problem\"] for r in dataset])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for b in bad_problems:\n",
    "    results = rag.top_k(b[\"problem\"], k=3)\n",
    "\n",
    "    bad_index = results[0][\"idx\"]\n",
    "    sim_score = results[0][\"score\"]\n",
    "    assert sim_score >= 0.99, \"Similarity score should be greater than 0.99\"\n",
    "\n",
    "    good_problems[bad_index] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Filter out bad problems\n",
    "good_dataset = [dataset[i] for i, good in enumerate(good_problems) if good]\n",
    "print(len(good_dataset))\n",
    "# Save the good dataset\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"primeintellect.json\")\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(good_dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
